package com.ncf.rpc.common;

import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
/**
 * 解码器
 * 主要做数据通信解码
 * @author crazy
 *
 */
public class NCFDecoder extends ByteToMessageDecoder{

	private Class<?> genericClass;
	// 构造函数传入向反序列化的class
	public NCFDecoder(Class<?> genericClass) {
		this.genericClass = genericClass;
	}
	
	
	
	@Override
	public final void decode(ChannelHandlerContext ctx, ByteBuf in,
			List<Object> out) throws Exception {
		if (in.readableBytes() < 4) {
			return;
		}
		
		in.markReaderIndex();
		int dataLength = in.readInt();
		if(dataLength < 0){
			ctx.close();
		}
		
		if(in.readableBytes() < dataLength) {
			in.resetReaderIndex();
		}
		
		//将ByteBuf转换为byte[]
		byte[] data = new byte[dataLength];
		in.readBytes(data);
		
		//将data转换成object
		Object obj = SerializationUtil.deserialize(data, genericClass);
		out.add(obj);
	}

}
